package com.jh3502.helper.utils;

import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 号型匹配工具类
 * Created by harisucici on 2017/6/13.
 */

public class UtilsCode {

    private static UtilsCode utilsCode;

    public static synchronized UtilsCode getInstance() {
        if (utilsCode == null) {
            utilsCode = new UtilsCode();
        }
        return utilsCode;
    }

    @NonNull
    public Map<String, Object> getHeight(int sexId, String s) {
        double height = Utils.getInstance().round(s, 0);
        Map<String, Object> map = new HashMap<>(2);
        map.put("index", 0);
        map.put("tall", height);
        int heightTemp;
        if (sexId == 0) {
            heightTemp = 155;
        } else {
            heightTemp = 145;
        }
        for (int i = 0; i < 7; i++) {

            double temp = Utils.getInstance().sub(heightTemp + 5 * i, height);
            if (Math.abs(temp) <= 2) {
                map.put("index", i);
                map.put("tall", heightTemp + 5 * i);
                return map;
            }
        }
        return map;
    }


    public String getChest(@NonNull List<String> list, String s) {
        double chest = Double.parseDouble(s);
        for (int i = 0; i < list.size() - 1; i++) {
            double a = Utils.getInstance().sub(chest, Double.parseDouble(list.get(i)));
            double b = Utils.getInstance().sub(chest, Double.parseDouble(list.get(i + 1)));
            if (Double.parseDouble(list.get(list.size() - 1)) < chest || chest < Double.parseDouble(list.get(0))) {
                if (a + 2 >= 0 && b - 2 <= 0) {
                    if (Math.abs(Utils.getInstance().sub(a, 1)) < Math.abs(Utils.getInstance().add(b, 2))) {
                        return list.get(i);
                    } else {
                        return list.get(i + 1);
                    }
                }
            } else {
                if (a >= 0 && b <= 0) {
                    if (chest == Double.parseDouble(list.get(i))) {
                        return list.get(i);
                    } else {
                        if (Math.abs(a) >= Math.abs(b)) {
                            return list.get(i + 1);
                        } else if (Math.abs(a) < Math.abs(b)) {
                            return list.get(i);
                        }
                    }
                }
            }
        }
        return Double.toString(chest);
    }

    public String getWaist(@NonNull List<String> list, String s) {
        double waist = Double.parseDouble(s);
        double waistTemp;
        for (int i = 0; i < list.size(); i++) {
            waistTemp = Double.parseDouble(list.get(i));
            if (waist == waistTemp) {
                return list.get(i);
            }
            double tempA = Utils.getInstance().add(Double.parseDouble(list.get(0)), 4 * i);
            double temp = Utils.getInstance().sub(tempA, waist);
            if (Math.abs(temp) < 2) {
                return list.get(i);
            }
            if (waist < Double.parseDouble(list.get(0)) || waist > Double.parseDouble(list.get(list.size() - 1))) {
                if (Math.abs(temp) == 2) {
                    return list.get(i);
                }
            }
            if (Double.parseDouble(list.get(i)) > waist && waist > Double.parseDouble(list.get(0))) {
                if (Math.abs(temp) == 2) {
                    return list.get(i);
                }
            }

        }
        return Double.toString(waist);
    }

    @NonNull
    public List<Integer> getRangeIndex(@NonNull List<String> rangeList, String string) {
        int data = Integer.parseInt(string);
        List<Integer> rangeIndex = new ArrayList<>(2);
        int index = 0;
        for (String tallRange : rangeList) {
            String[] s = tallRange.split("~");
            if (data == Integer.parseInt(s[0])) {
                rangeIndex.add(index);
            }
            if (data > Integer.parseInt(s[0]) && data < Integer.parseInt(s[1])) {
                rangeIndex.add(index);
            }
            if (data == Integer.parseInt(s[1])) {
                rangeIndex.add(index);
            }
            index++;

        }
        return rangeIndex;
    }
}
